﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace StudentSynchronization.Daemon
{
    /// <summary>
    /// 周期任务
    /// </summary>
    public class QuartzWork
    {

        //0 0/1 * * * ?
        public void Start()
        {

            // 启动后台线程
            Thread backgroundThread = new Thread(StartBackgroundThread);
            backgroundThread.IsBackground = true; // 设置为后台线程
            backgroundThread.Start();
        }
        public static WinProcess winpro = new WinProcess();
        private static void StartBackgroundThread()
        {
            try
            {


                // 保持后台线程运行
                while (true)
                {
                    ProcessData.processlist.Where(p => p.IsRestart).ToList().ForEach(pro =>
                    {
                        Task.Run(() =>
                        {
                            try
                            {
                                var profullname = pro.Path + Path.DirectorySeparatorChar + pro.Name;
                                if (File.Exists(profullname))
                                {
                                    var findpro = Process.GetProcessesByName(pro.Name.Replace(".exe", "")).FirstOrDefault(p => p.MainModule.FileName == pro.Path + Path.DirectorySeparatorChar + pro.Name);

                                    if (findpro == null || findpro.Id == 0)
                                    {

                                        winpro.SetForm(pro.Path + Path.DirectorySeparatorChar + pro.Name, pro.Path, pro.Name);

                                        Thread.Sleep(500);

                                        findpro = Process.GetProcessesByName(pro.Name.Replace(".exe", "")).FirstOrDefault(p => p.MainModule.FileName == pro.Path + Path.DirectorySeparatorChar + pro.Name);
                                    }
                                    if (ProcessData.needwindows == "true")
                                    {
                                        ProLogic.Instance.ReStartPro(pro.IndexNum, findpro.Id, 0);
                                    }
                                }
                                else if (pro.Info != "配置错误")
                                {
                                    pro.Info = "配置错误";
                                    if (ProcessData.needwindows == "true")
                                    {
                                        ProLogic.Instance.UpdatePro(pro.IndexNum);
                                    }
                                }
                            }
                            catch
                            {
                            }
                        });
                    });
                    if (float.TryParse(ProcessData.timecron, out float timeout))
                        Thread.Sleep((int)(timeout * 1000)); // 每秒检查一次
                    else
                        Thread.Sleep(5000);
                }
            }
            catch (Exception ex)
            {
            }
        }





    }
}
